﻿<!doctype html>
<html><!-- InstanceBegin template="/Templates/template1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta charset="utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>岗位设置</title>
<!-- InstanceEndEditable -->
<script type="text/javascript" src="../js/common.js"></script>
<link href="../css/Pager.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery.pager.js"></script>
<script type="text/javascript" src="../js/gbRst.js"></script>
<script type="text/javascript" src="../js/utils.js"></script>
<script type="text/javascript" src="../js/formValidator.js"></script>
	<script type="text/javascript" src="../js/formValidatorRegex.js"></script>
<link href="../css/style.css" type="text/css" rel="stylesheet" />
<link href="../css/validator3.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../js/checkLogin.js"></script>
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<script type="text/javascript" >
$(function(){
	refreshInput($(".serWrapper"));
	$.formValidator.initConfig({
		formID : "mainForm",
		onError : function() {
			alert("校验没有通过，具体错误请看错误提示");
		}
	});	
	$("#dutyName").formValidator({
		empty:false,
		onshow:"请输入名称",
		onfocus:"请输入字符",
		oncorrect:"正确"
		 }).inputValidator({
				min:1,
				max:50,
				onerror:"不能为空(最大长度为50)"
			}).regexValidator({
				regexp:"^[A-Za-z0-9-_\u4e00-\u9fa5]{1,20}$",
				onerror:"请输入合法字符"});
	$("#dutyCode").formValidator({
		empty:false,
		onshow:"请输入编码",
		onfocus:"请输入字符",
		oncorrect:"正确"
		 }).inputValidator({
				min:1,
				max:20,
				onerror:"不能为空(最大长度为20)"
			}).regexValidator({
				regexp:"^[A-Za-z0-9-_\u4e00-\u9fa5]{1,20}$",
				onerror:"请输入合法字符"});
// 	$("#dutyCode").blur(function(){
// 		var dutyCode=$("#dutyCode").val()
// 		if(IsEmpty(dutyCode)){
// 			$("#dutyCodeTip").html("不能为空");
// 			return false;
// 		}
// 		var id=$("#updateId").val();
// 		$("#dutyCodeTip").html("正在验证");
// 		$("#close_this").attr("disabled",true);
// 		$.post("../duty/check/checkDutyCode",{"id":id,"dutyCode":dutyCode}, function(data) {
// 			var dt = JSON.parse(data);
// 			if(dt.state==0){
// 				$("#dutyCodeTip").html("可以使用");
// 				$("#close_this").attr("disabled",false);
// 				}else{
// 					$("#dutyCodeTip").html("该编码已被使用");
// 					$("#dutyCode").focus();
// 					}	
// 		});
// 	})
});
function bclicks(obj){
	var $first=obj.parent("td").parent("tr").children("td:first");
	var id=$first.html();
	var name=$first.next().html();
	//$("#shopName").children("option").blur();
	getParamToshopS(id,name)
}
var pageIndex = 1;
var pagecount = 0;
var count = 0;
var shopId="shopId";
var shopPageCount=0;
var shopCount=0;
$(function(){
	$("#shopIsChoosed").click(function(){
		if($("#shopIsChoosed").is(":checked")){
			$("#shopName").attr("disabled",false);
		}else{
			$("#shopName").attr("disabled",true);
		}
	})
	addMargin($("#window_change"));
})

function toAdd(){
	$("#doflag").val(1);
	$("#yinying1,#window_change").show();

}
function toUpdate(id,shopId,obj){
	$("#doflag").val(2);
	$("#updateId").val(id)
	var dutyName=obj.parent().prev().html();
	var dutyCode=obj.parent().prev().prev().html();
	var shopName=obj.parent().prev().prev().prev().html();

	var option="<option value='"+shopId+"'>"+shopName+"</option>";
	$("#shopName2").html(option);
	$("#dutyName").val(dutyName);
	$("#dutyCode").val(dutyCode);
	$("#yinying1,#window_change").show();
}

function getParamToshopS(id,name){
	var htm="<option value='"+id+"'>"+name+"</option>"
	var shopflag=$("#shopflag").val();
	if(shopflag==1){
	$("#shopName").html(htm);
	$("#shopName").val(id);
	}else{
		$("#shopName2").html(htm);
		$("#shopName2").val(id);	
	}
	closeShopShow();
}

function closeShopShow(){
	$("#shopShow,#yinying").hide();
	$("#searchName").val("");
}

function InitShopList(shopPageIndex){
	var shopName=$("#searchName").val();
	$.ajax({
		type : "post",
		async:false,
		data :'name='+shopName,
		datatype : "json",
		url : "../shop/find/shopList/" + shopPageIndex + "/5",
		success : function(data) {
			var rst = JSON.parse(data);
			if(IsEmpty(rst)){
				art.dialog({fixed:true, lock: true,content:"查询出错",icon:'error',ok:true});	
				return false;
				}
				if(rst.state==0){
					$("#shopList tbody tr").remove();

					initShopTable(rst);
					$("table[id='shopList'] tbody tr").each(function(){
						$(this).children("td:last").css("text-align","center").append("<input type='button' class='common_btn' value='选择' onclick='bclicks($(this))'>");

					})	
					if(rst.result.len > 0){
						 var shu = rst.result.len;
						 shopCount = Math.ceil(shu/5);					
						 }else{
								shopCount==0;
						 }
					 $("#shopPager").pager({ pagenumber: ++shopPageIndex, pagecount: shopCount, buttonClickCallback: ShopPageClick });
					 addMargin($("#shopShow"));	
					 }else if(rst.state==1){
					art.dialog({fixed:true, lock: true,content:rst.msg,icon:'error',ok:true});	
				}else{
					checkStateShowErrorMsgAndDealResult(data);
				}
		}	
	});	
}

function InitTable() {
	var code=$("#code").val().replace(/\s/gi,'');
	var shopId="";
	var param=null;
	if($("#shopIsChoosed").is(":checked")){
		shopId=$("#shopName").val();
		if(shopId!=""&&shopId!=null){
			param={'code':code,'shopId':shopId};
		}else{
			param={'code':code}
		}
	}else{
		param={'code':code}
	}
	$.ajax({
		type : "get",
		async:false,
		data :param,
		datatype : "json",
		url : "../duty/find/dutyList/" + pageIndex + "/10",
		success : function(data) {
			var rst = JSON.parse(data);

					if(IsEmpty(rst)){
						art.dialog({fixed:true, lock: true,content:"查询出错",icon:'error',ok:true});	
						return false;
						}
						if(rst.state==0){
							$("#memberList tbody tr").remove();
							initDutyTable(rst);	
							if(rst.result.len > 0){
								 var shu = rst.result.len;
							 	 pagecount = Math.ceil(shu/10);
							 	 $("#NoResult").hide();
							}else{
								pagecount=0;
								$("#NoResult").show();
							}
							 	$("#sup").html("共"+rst.result.len+"条");	
							 $("#pager").pager({ pagenumber: pageIndex, pagecount: pagecount, buttonClickCallback: PageClick });
								
							 }else if(rst.state==1){
							art.dialog({fixed:true, lock: true,content:rst.msg,icon:'error',ok:true});	
						}else{
							checkStateShowErrorMsgAndDealResult(data);
						}				
		}	
	});
}

ShopPageClick = function(pageclickednumber) {
    $("#shopPager").pager({ pagenumber: --pageclickednumber, pagecount: pagecount, buttonClickCallback: PageClick });
    $("#result").html("Clicked Page " + pageclickednumber);
    InitShopList(pageclickednumber);
}

PageClick = function(pageclickednumber) {
	pageIndex=pageclickednumber
	InitTable();
}

function search(){
	InitTable();
}
function search2(){
	InitShopList(0);
}
function initDutyTable(json){
	if(json!=null && json.state==0){
		var params = new Array("ShopName", "Code","Name");// "PayCash", "PayPos", "PayDepstAmt", "PayCheck", "PayPersonCard", "PayIntAmtMny", "StaName", "PaidName"
		LoadDutytable(json,params,"memberList");
	}
}



function initShopTable(json){
	if(json!=null && json.state==0){
		var params = new Array("id", "name","mp","contact");
		Loadtable(json,params,"shopList");
	}
}


function LoadDutytable(json, params, tableId){
	var phArray = new Array();
	var count = 0;
	if (params != null) {
		$(params).each(function(pIndex, pData) {
			$.each(json.result.head, function(hIndex, hData) {
				if (pData == hData) {
					phArray[count++] = hIndex;
				}
			});
		});
	}
	if (json.result.data != null) {
		$.each(json.result.data, function(index, dt) {
			$("table[id='" + tableId + "']").append("<tr></tr>");
			$(phArray).each(function(phIndex, data) {
				$.each($(dt), function(dtIndex, d) {
					if (dtIndex == data) {
						$("table[id='" + tableId + "'] tr:last").append("<td>" +  (d === null||d=="null" ? " " : d) + "</td>");
					}
				});
			});
			var id=getHeadIndexValue(index, json, "id");
			var htm="<td><input class=\"common_btn to_update\" type=\"button\" value=\"修改\" onclick='toUpdate("+id+","+getHeadIndexValue(index, json, "ShopID")+",$(this))' ><br><input class=\"del\" type=\"button\" value=\"删除\" onclick='popPoint("+id+")' ><br><input class=\"common_btn check_btn\" type=\"button\" value=\"权限配置\" onclick='toSetPost("+id+")' ></td>"
			$("table[id='" + tableId + "'] tr:last").append(htm);
		});
	}
}

function toSetPost(id){
	window.location.href='postDoing.html?id='+id+"&menu="+getIdByName("menu")+"&submenu="+getIdByName("submenu")
}
function Loadtable(json, params, tableId){
	var phArray = new Array();
	var count = 0;
	if (params != null) {
		$(params).each(function(pIndex, pData) {
			$.each(json.result.head, function(hIndex, hData) {
				if (pData == hData) {
					phArray[count++] = hIndex;
				}
			});
		});
	}
	if (json.result.data != null) {
		$.each(json.result.data, function(index, dt) {
			$("table[id='" + tableId + "']").append("<tr></tr>");
			$(phArray).each(function(phIndex, data) {
				$.each($(dt), function(dtIndex, d) {
					if (dtIndex == data) {
						$("table[id='" + tableId + "'] tr:last").append("<td>" +  (d === null||d=="null" ? " " : d) + "</td>");
					}
				});
			});
		});
	}
}

function showShopList(type){
	$("#shopflag").val(type)		
		$("#shopName").blur();
		$("#shopName2").blur();
	$("#shopShow,#yinying").show();
	InitShopList(0);
}

function  downRescouse(){
var code=$("#code").val().replace(/\s/gi,'');
	var shopId="";
	var param='code='+code;
	if($("#shopIsChoosed").is(":checked")){
		shopId=$("#shopName").val();
		if(shopId!=""&&shopId!=null){
			param+="&shopId="+shopId;
		}
	}
	param+="&downType=8"
	window.location.href="../csvdown/down/csv/down?"+param
}

function resetForm(){
	$("#close_this").attr("disabled",false)
	$("#shopName2").html("");
	$("#dutyCode").val("");
	$("#dutyName").val("");
	$("#updateId").val("");
}

function saveInfo(){
	var doflag=$("#doflag").val();
	if(doflag==1){
		if(!$.formValidator.pageIsValid()){
			return false;
		}
	 		var dutyCode=$("#dutyCode").val();
	 		var id=$("#updateId").val();
	 		$("#dutyCodeTip").html("正在验证");
	 		$("#close_this").attr("disabled",true);
	 		$.post("../duty/check/checkDutyCode",{"id":id,"dutyCode":dutyCode}, function(data) {
	 			
	 				$("#close_this").attr("disabled",false);
	 			var dt = JSON.parse(data);
	 			if(dt.state==0){
	 				$("#dutyCodeTip").html("可以使用");
                		addInfo();
	 				}else{
	 					$("#dutyCodeTip").html("该编码已被使用");
	 					art.dialog({fixed:true, lock: true,content:"该编码已被使用",icon:'error',ok:true});	

	 					$("#dutyCode").focus();
	 					return false;
	 					}	
	 		});
	}else if(doflag==2){
		updateInfo()
	}else{
			art.dialog({fixed:true, lock: true,content:"页面出错",icon:'error',ok:true});	
		window.location.reload();
	}
}

function addInfo(){
	if(!$.formValidator.pageIsValid()){
		return false;
	}
	var shopId=$("#shopName2").val();
	var dutyCode=$("#dutyCode").val().replace(/\s/gi,'');
	var dutyName=$("#dutyName").val().replace(/\s/gi,'');
	var heads=new Array("shopId","name","code")
	var datas=new Array(shopId,dutyName,dutyCode);
	if(IsEmpty(shopId)){
		art.dialog({fixed:true, lock: true,content:"请选择店铺",icon:'error',ok:true});
		return false;
	}
	var parser = new Object();
	var jsonParserStr = null;
	if (IsNotEmpty(datas)){
		addToParser(heads, datas, "add" , parser);
		jsonParserStr =  JSON.stringify(parser);
	}
	$.post("../duty/insert/insertDuty",{"data":jsonParserStr}, function(data) {
		var dt = JSON.parse(data);
		if(IsEmpty(dt)){
			art.dialog({fixed:true, lock: true,content:"数据添加错误",icon:'error',ok:true});	
			return false;
			}
			if(dt.state==0){
				pageIndex=1;
//	 			alert(33)
				InitTable();
				art.dialog({fixed:true, lock: true,content:"添加成功",icon:'succeed',ok:true});	
				$("#yinying1,#window_change").hide();
				resetForm();
				 }else if(dt.state==1){
				art.dialog({fixed:true, lock: true,content:"数据添加错误",icon:'error',ok:true});	
			}else{
				checkStateShowErrorMsgAndDealResult(data);
			}		
	});
}
function deleteDuty(id){

	$.post("../duty/del/deleteDuty",{"id":id}, function(data) {
		var dt = JSON.parse(data);
		if(IsEmpty(dt)){
			art.dialog({fixed:true, lock: true,content:"数据删除错误",icon:'error',ok:true});	
			return false;
			}
			if(dt.state==0){
				InitTable();
				art.dialog({fixed:true, lock: true,content:"删除成功",icon:'succeed',ok:true});	
				 }else if(dt.state==1){
						art.dialog({fixed:true, lock: true,content:"数据删除错误",icon:'error',ok:true});	
			}else{
				checkStateShowErrorMsgAndDealResult(data);
			}
	});
}

function updateInfo(){
	if(!$.formValidator.pageIsValid()){
		return false;
	}
	var id=$("#updateId").val()
	if(IsEmpty(id)){

		art.dialog({fixed:true, lock: true,content:"修改出错",icon:'error',ok:true});	
		return false;
	}
	var shopId=$("#shopName2").val();
	var dutyCode=$("#dutyCode").val().replace(/\s/gi,'');
	var dutyName=$("#dutyName").val().replace(/\s/gi,'');
	
	var heads=new Array("shopId","name","code","id")
	var datas=new Array(shopId,dutyName,dutyCode,id);
	var parser = new Object();
	var jsonParserStr = null;
	if (IsNotEmpty(datas)){
		addToParser(heads, datas, "edit" , parser);
		jsonParserStr =  JSON.stringify(parser);
	}
	$("#close_this").attr("disabled",true)
	$.post("../duty/edit/editDuty",{"data":jsonParserStr}, function(data) {
		$("#close_this").attr("disabled",false)
		var dt = JSON.parse(data);
		if(IsEmpty(dt)){
			art.dialog({fixed:true, lock: true,content:"修改出错",icon:'error',ok:true});	
			return false;
			}
			if(dt.state==0){
				art.dialog({fixed:true, lock: true,content:"修改成功",icon:'succeed',ok:true});	
				InitTable();
				$("#yinying1,#window_change").hide();
				resetForm();
			}else if(dt.state==1){
				art.dialog({fixed:true, lock: true,content:"修改出错",icon:'error',ok:true});	
			}else{
				checkStateShowErrorMsgAndDealResult(data);
			}
	});
}
function popPoint(id){
	art.dialog({fixed:true, lock: true,content:"确认删除该条数据？",icon:'question',ok: function(){ deleteDuty(id)},cancel:true});
	}

</script>
</head>
<body>
<div id="yinying1"></div>
<div id="yinying"></div>
<div id="shopShow" class="hideDiv">
    <div style="float:right;margin-bottom:5px;width:20px;height:20px;line-height:20px;text-align:center;background-color:#eee;margin-bottom:5px;border:1px solid #ccc;" > <span style="cursor: pointer;font-size: 14px;" onclick="closeShopShow();" >X</span> </div>
    <div class="bottom_search"><div class="serWrapper"><input type="text" id="searchName" class="txt"><strong class="refresh" onclick="$('#searchName').val('')">清空</strong></div><input class="common_btn" type="button" style="width:auto;margin-left:5px;" value="查询" onclick="search2()"></div>
    <table   id="shopList" cellpadding="4" cellspacing="0" border="1" width="100%">
    <thead>
    <tr>
    <th>店面ID</th>
    <th>店面名称</th>
    <th>电话</th>
    <th>操作</th>
    </tr>
    </thead>
    </table>
    	<div id="shopPager"></div>
    </div>
<!-- InstanceBeginEditable name="EditRegion3" -->
<form id="mainForm" >
  <div id="window_change" class="member_search hideDiv">
    <div class="closeTop"><span class="closeThe" onclick="closeParent($(this))"><img src="../images/close.png"></span></div>
    <input type="hidden" id="doflag">
    <input type="hidden" id="shopflag">
    <input type="hidden" id="defaultCflag">
    <input type="hidden" id="updateId">
    <p>
      <label><span> 店面：</span>
        <select id="shopName2" onclick="showShopList(2)" style=";height:20px;">
        	<option value="">请选择店铺</option>
        </select>
      </label><div id="shopName2Tip"></div>
    </p>
    <p>
      <label><span>岗位编号：</span>
        <input type="text" id="dutyCode"><strong id="dutyCodeTip" class="onError"></strong>
      </label>
    </p>
    <p>
      <label><span>岗位名称：</span>
        <input type="text" id="dutyName"><strong id="dutyNameTip" class ="onError"></strong>
      </label>
    </p>
    <div class="submit_btn">
      <input type="button" value="保存信息" class="common_btn" id="close_this" onclick="saveInfo()">
    </div>
  </div>
</form>
<div class="container" id="container">
  <div class="container_R fr">
	<div class="place">岗位设置</div>

    <div class="member_search">
    <span> <input type="checkbox" id="shopIsChoosed" style="width:auto;height:auto;border:none;"> 店面：</span><select id="shopName" onclick="showShopList(1)" style=";height:20px;" disabled="disabled" >  </select>
    <span>岗位代码：</span> <input type="text" id="code" class="absoluteTxt"><input type="button" value="查询" class="btn" onclick="search()"></div>
    
    <table width="100%" class="member_list" cellpadding="0" cellspacing="1" border="0" id="memberList">
    	<caption><strong>岗位列表        <span id="sup">共0条</span></strong><input type="button" value="增加岗位" class="common_btn addList" onclick="toAdd()"></caption>
    	<thead>
    	<tr>
        	<th>店铺名称</th>
        	<th>岗位编号</th>
        	<th>岗位名称</th>
        	<th>操作</th>
        </tr>
        </thead>
    </table>
    <div id="NoResult"  class="Nothing" style="text-align:center;display: none;">无符合条件的数据！</div>
    <div id="pager" style="float: right;"></div>
  </div>
  <div class="fc"></div>
</div>

<!-- InstanceEndEditable -->
<script type="text/javascript" src="../js/header.js"></script>
<script type="text/javascript" src="../js/left.js"></script>
<script type="text/javascript" src="../js/footer.js"></script>
<script type="text/javascript">
clearThis("absoluteTxt");
showTab(1,4);
</script>
</body>
<!-- InstanceEnd --></html>
